package sw6.visualschedule.adminpanel;

import java.util.ArrayList;
import java.util.List;

import sw6.visualschedule.R;
import sw6.visualschedule.entities.ScheduleActivity;
import sw6.visualschedule.persistence.ScheduleActivityRepo;
import tools.BundleHelper;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Lists the activities of the application.
 */
public class Activities extends ListActivity {

	private transient List<ScheduleActivity> mMenuItems = new ArrayList<ScheduleActivity>();
	private transient ArrayAdapter<ScheduleActivity> mAdapter;
	private final transient ScheduleActivityRepo mActivityService = new ScheduleActivityRepo(this);
	
	@Override
    public void onResume() {
		super.onResume();
		
		//Get activities and create adapter to populate menu list
		mMenuItems = new ArrayList<ScheduleActivity>( mActivityService.getAll() );						
		mAdapter = new ArrayAdapter<ScheduleActivity>(this, android.R.layout.simple_list_item_1, mMenuItems);		
		setListAdapter(mAdapter);
		
		//Create long click listener on listactivity's listview
		final ListView list = getListView();
		list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
		    @Override
		    public boolean onItemLongClick(final AdapterView<?> adapter, final View view, final int pos, final long itemId) {
		        //Call click handler method added to ListActivity
		    	return onLongListItemClick(view, pos, itemId);
		    }
		});
	}
	/**
	 * On long list item click, the activity clicked should be removed, given the user acknowledges this. 
	 */
	protected boolean onLongListItemClick(final View view, final int pos, final long itemId) {
		//Get ScheduleActivity id
		final ScheduleActivity selectedActivity = mMenuItems.get(pos);
		
		//Show confirm dialog
		new AlertDialog.Builder(this)
		.setTitle(R.string.dd_delete_activity)
		.setMessage(getString(R.string.dd_do_you_want_to_delete, selectedActivity.getTitle()))
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
		    public void onClick(final DialogInterface dialog, final int whichButton) {
		        //User clicked yes
		    	deleteActivity(selectedActivity);
		    }
		 })
		 .setNegativeButton(android.R.string.no, null).show();

	    return true;
	}
	
	private void deleteActivity(final ScheduleActivity scheduleActivity) {

		//Remove from list
		mAdapter.remove(scheduleActivity);
		mAdapter.notifyDataSetChanged();
		
		//Remove from database
    	mActivityService.delete(scheduleActivity);

    	//User feedback
    	Toast.makeText(this, getString(R.string.delete_activity) +" "+ scheduleActivity.getTitle(), Toast.LENGTH_SHORT).show();
	}
	
	@Override
	protected void onListItemClick(final ListView list, final View view, final int position, final long itemId) {
		
		super.onListItemClick(list, view, position, itemId);
		
		final ScheduleActivity selectedActivity = mMenuItems.get(position);
		
		final Intent intent = new Intent(this, ActivityConstructor.class);
		
		intent.putExtra(BundleHelper.ACTIVITY_ID, selectedActivity.getId());
		startActivity(intent);
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
	    final MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.activities_pref, menu);
	    
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {

	    boolean result = false;
	    
	    if (item.getItemId() == R.id.add_activity) {
	    	final Intent intent = new Intent().setClass(this, ActivityConstructor.class);
	    	startActivity(intent);
	        result = true;
	    }
	    else {
	        result = super.onOptionsItemSelected(item);
	    }
	    
	    return result;
	}	
	
	
}